package cn.xzqwjw.taskmanager.controller.api;

import cn.xzqwjw.taskmanager.service.SysAdminRoleService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import javax.validation.constraints.Min;
import javax.validation.constraints.NotBlank;
import javax.validation.constraints.NotNull;

/**
 * @author rush
 */
@RestController
@RequestMapping("/api/admin-role")
@Validated
public class SysAdminRoleController {

  private final SysAdminRoleService sysAdminRoleService;

  @Autowired
  public SysAdminRoleController(SysAdminRoleService sysAdminRoleService) {
    this.sysAdminRoleService = sysAdminRoleService;
  }

  /**
   * 处理 "/api/admin_role" 的 PATCH 请求，修改某角色下所有管理员
   */
  @PatchMapping("")
  public String modifyAdminIdsByRoleId(
      @NotNull(message = "id 不能为空")
      @Min(value = 1, message = "id 必须大于0")
      @RequestParam(value = "RoleId") Integer idRole,
      @NotBlank(message = "参数不能为空")
      @RequestBody String paramsJson) {
    return sysAdminRoleService.modifyAdminIdsByIdRole(idRole, paramsJson);
  }

  /**
   * 处理 "/api/admin_role" 的 Get 请求，根据角色id查询得到属于此角色的所有管理员
   */
  @GetMapping("")
  public String getAdminIdsByRoleId(
      @NotNull(message = "id不能为空")
      @Min(value = 1, message = "id必须大于0")
      @RequestParam(value = "RoleId") Integer idRole) {
    return sysAdminRoleService.listAdminIdByIdRole(idRole);
  }

}
